/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package management;

import java.sql.*;

/**
 *
 * @author Windows7
 */
public class MyConnection {

    static Connection con = null;
    static Statement stm = null;
    static  CallableStatement calSta = null;

    public static Connection getConnection() {

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection("jdbc:sqlserver://ADMIN-PC\\SQLEXPRESS:1433;databaseName=Management_Canteen", "sa", "123456");
        } catch (Exception e) {
            return null;
        }

        return con;
    }

    public static Statement getStatement() {
        try {
            stm = getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            return stm;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ResultSet getResultSet(String sql) {
        try {
            ResultSet rs = getStatement().executeQuery(sql);
            return rs;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;//if have error return null
    }
//      public static int executeQuery(String sql) {
//        try {
//            int result = getStatement().executeUpdate(sql);
//            return result;
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//        }
//        return -1; //if error return -1

    public static void executeQuery(String sql) {
        try {
            int result = getStatement().executeUpdate(sql);
            // return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        // return -1; //if err
    }

    public static ResultSet queryResultSet(String sql) {
        ResultSet rs = null;
        try {
            Statement st = getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery(sql);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return rs;
        }
    }
}
